package org.oimwrapper.connectors;

import Thor.API.Base.tcUtilityOperationsIntf;
import Thor.API.Exceptions.tcAPIException;
import com.thortech.xl.client.events.*;
import com.thortech.xl.dataaccess.*;
import com.thortech.xl.dataobj.*;

/**
 * Base class for creating event handlers.
 */
public abstract class OIMEventHandlerBase extends tcBaseEvent {

	/**
	 * The log category to use for this event handler.
	 */
	private String logCategory;
	
	/**
	 * The connection to OIM.
	 */
	private OIMAdapterBase oim;
	
	/**
	 * Default Constructor.
	 * 
	 * @param LogCategory The log category to use for this event handler.
	 */
	public OIMEventHandlerBase(String LogCategory) {
		logCategory = LogCategory;
		String[] classNamePieces = getClass().getName().split("\\.");
		setEventName(classNamePieces[classNamePieces.length - 1]);
		oim = null;
	}
	
	/**
	 * Helper class to create an adapterBase instance.
	 */
	private final class eventHandlerAdapter extends OIMAdapterBase {
		
		/**
		 * Default Constructor.
		 * 
		 * @param LogCategory The log category to use for this event handler.
		 * @param Database The database reference from the calling adapter.
		 */
		protected eventHandlerAdapter(String LogCategory, tcDataProvider Database) {
			super(LogCategory, Database);
		}
	}

	/**
	 * This method is called by the data object manager to run this adapter.
	 * 
	 * @exception Exception
	 */
	protected final void implementation() throws Exception {
		try {
			execute(getDataObject());
		} catch (Exception e) {
			OIMExceptionHandler.handleException(oim.logger, e, getClass());
		}
	}
	
	/**
	 * Gets a utility instance.
	 * 
	 * @param Utility The name of the utility to get.
	 * @return The utility object.
	 * @exception tcAPIException
	 */
	protected final tcUtilityOperationsIntf getUtility(String Utility) throws tcAPIException {
		try {
			return oim.getUtility(Utility);
		} catch (Exception e) {
			throw new tcAPIException(e.getMessage(), e);
		}
	}
	
	/**
	 * Gets a utility instance as a specific user.
	 * 
	 * @param Utility The name of the utility to get.
	 * @param Username The username to get the utility instance under.
	 * @return The utility object.
	 * @exception tcAPIException
	 */
	protected final tcUtilityOperationsIntf getUtility(String Utility, String Username) throws tcAPIException {
		try {
			OIMRemoteClientBase remote = new OIMRemoteClientBase(logCategory);
			remote.setConnectionInfo(Username);
			remote.connect();
			return remote.getUtils().getUtility(Utility);
		} catch (Exception e) {
			throw new tcAPIException(e.getMessage(), e);
		}
	}

	/**
	 * Indicates that an error is detected in the data and that the corresponding call
	 * causing the data to be modified should be rejected. 
	 * 
	 * @param Error The error to be returned to the user.   The error should match
	 *              an error definition as set in the design console.
	 */
	public final void handleError(String Error) {
		super.handleError(Error);
	}
	
	/**
	 * Gets the adapterBase object corresponding to this OIM connection.
	 * 
	 * @return The adapterBase object.
	 */
	public final OIMAdapterBase getAdapterBase() {
		if (oim == null) {
			oim = new eventHandlerAdapter(logCategory, getDataBase());
		}
		return oim;
	}
	
	/**
	 * Called to execute the adapter.
	 * 
	 * @param DataObject The dataobject being processed by this adapter.
	 * @exception Exception
	 */
	public abstract void execute(tcDataObj DataObject) throws Exception;
}